#!/bin/sh

export TEXTDOMAIN=frisbee
export OUTPUT_CHARSET=UTF-8
. gettext.sh

function connect_func () {
	export TEXTDOMAIN=frisbee
	export OUTPUT_CHARSET=UTF-8
	. gettext.sh
	ps aux | grep -q 'wvdial isp[12]' \
	 || rm -f /tmp/.network_tray-use_analog_dialup_icons 2>/dev/null #130505
	echo -e "$(eval_gettext "Attempting connection through \$GPRSDEV to APN \$GPRSAPN...\n")" #130812
	if [ ! -x /usr/sbin/resolvconf ];then #160116...
	 if [ -x /usr/sbin/networkdisconnect ];then #but not ppp interface.
	  /usr/sbin/networkdisconnect
	 else 
	  [ -n "$(busybox pidof wpa_supplicant)" ] \
	   && wpa_cli terminate #kills any running wpa_supplicant
	  dhcpcd -k
	 fi
	fi
	[ -e /dev/ppp ] || mknod /dev/ppp c 108 0
	# Start PPP daemon...
	# For sanity, keep a lock on the serial line (lock).
	logger "frisbee: $(eval_gettext "Starting PPP daemon for \$GPRSDEV")"
	/usr/sbin/pppd $GPRSDEV \
	 call gprs-generated \
	 connect "chat -v -T $GPRSNBR -f /etc/ppp/chatscripts/gprs-connect-chat" \
	 disconnect "chat -v -s -S -f /etc/ppp/chatscripts/gprs-disconnect-chat" \
	 linkname gprs \
	 nodetach \
	 lock
	local STATUS=$?
	echo "$(eval_gettext "Exit status is \$STATUS")" #130812
	echo "$(gettext 'DISCONNECTED')"
	logger "frisbee: $(eval_gettext "pppd exit status for \$GPRSDEV is \$STATUS")"
	exit $STATUS
}
export -f connect_func

function connection_dialog_func () {
	local DISCSTOP="$(gettext 'DISCONNECT')"
	Xdialog --center --wmclass "frisbee" --title "Frisbee $(gettext 'GPRS Connection Log')" --backtitle "$(eval_gettext "NOTICE: If log shows a failure to connect, click '\$DISCSTOP' to exit.")" --ok-label "$DISCSTOP or stop trying" --cancel-label "$(gettext 'CLOSE window but stay online')" --fixed-font --tailbox /tmp/.gprs_connect.log 25 82
	local STATUS=$?
	if [ $STATUS -eq 0 -a -s /var/run/ppp-gprs.pid ];then
		frisbee-gprs-disconnect
	fi
	return $STATUS
}

. /root/.config/gprs.conf

if [ -s /var/run/ppp-gprs.pid ];then
	connection_dialog_func
	exit $? #0 = disonnect/stop, 1 = close but stay connected, 255 = abort
else
	echo "#Generated by frisbee-gprs-connect" > /etc/ppp/peers/gprs-generated
	echo "call gprs-editable" >> /etc/ppp/peers/gprs-generated
	if [ "$GPRSUSER" ];then
		echo -e "auth\nuser $GPRSUSER" >> /etc/ppp/peers/gprs-generated
		[ "$GPRSPAPONLY" = "true" ] \
		 && echo "require-pap" >> /etc/ppp/peers/gprs-generated
	else
		echo "noauth" >> /etc/ppp/peers/gprs-generated
	fi

	[ "$GPRSPIN" ] \
	 && SAVEVAL="AT+CPIN=\"$GPRSPIN\"" \
	 || SAVEVAL="AT"
	[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-cpin_command 2>/dev/null | grep -v ^\')" ] \
	 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cpin_command
	SAVEVAL="AT+CGDCONT=1,\"IP\",\"$GPRSAPN\""
	[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-cgdcont_command 2>/dev/null | grep -v ^\')" ] \
	 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cgdcont_command

	if [ -c $GPRSDEV ] \
	 && echo ATZ | microcom -t 200 $GPRSDEV 2>/dev/null | grep -q 'ATZ'; then #160104
		echo -n > /tmp/.gprs_connect.log
		connect_func 2>&1 > /tmp/.gprs_connect.log &
		if [ $? -eq 0 ];then
			connection_dialog_func #160116
			exit $? #0 = disonnect/stop, 1 = close but stay connected, 255 = abort
		else
			Xdialog --center --wmclass "frisbee" --title "Frisbee $(gettext 'GPRS Connection Log')" --backtitle "\n$(gettext 'The attempt to connect was unsuccessful:')\n" --no-ok --cancel-label $(gettext 'Close') --fixed-font --tailbox /tmp/.gprs_connect.log 25 82
		fi
	else
		Xdialog --center --wmclass "frisbee" --title "Frisbee $(gettext 'GPRS Connect')" --icon $ICONLIB/error.xpm --msgbox "\n$(gettext 'Cannot connect.')\n\n$(eval_gettext "Modem device '\$GPRSDEV' is not accessable.")\n$(gettext 'It might not be plugged in.')\n" 10 60
	fi
fi
exit 0
